﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;

namespace WindowsGame1
{
    class Connection
    {
        /// <summary>
        /// Dados da tabela do MySQL
        /// </summary>
        
        String _conexaoMySQL = "";
        MySqlConnection con = null;
        public Connection()
        {
            _conexaoMySQL = "server=www.freesql.org;user id=cursedzerow;password=i4gqix8w;database=omfg";
        }

        public int getHighscore()
        {
            try
            {
                String sql = "SELECT highscore_surv FROM tbHighscore";
                con = new MySqlConnection(_conexaoMySQL);
                MySqlCommand cmd = new MySqlCommand(sql, con);

                con.Open();

                MySqlDataReader dr;

                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    return Convert.ToInt32(dr["highscore_surv"]);
                }
            }
            catch (Exception ex)
            {
                //throw ex;
                return 0;
            }
            finally
            {
                con.Close();
            }
            return 0;
        }

        public void setHighscore(int a, string b)
        {
            try
            {
                String sql = "UPDATE tbHighscore SET highscore_surv = @high, nome_user_surv = @name ";
                con = new MySqlConnection(_conexaoMySQL);
                MySqlCommand cmd = new MySqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@high", a);
                cmd.Parameters.AddWithValue("@name", b);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                //meh
            }
            finally
            {
                con.Close();
            }
        }

        public string getName()
        {
            try
            {
                String sql = "SELECT nome_user_surv FROM tbHighscore";
                con = new MySqlConnection(_conexaoMySQL);
                MySqlCommand cmd = new MySqlCommand(sql, con);

                con.Open();

                MySqlDataReader dr;

                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    return dr["nome_user_surv"].ToString();
                }
            }
            catch (Exception ex)
            {
                //throw ex;
                return "none";
            }
            finally
            {
                con.Close();
                
            }
            return "connection problem";
        }

        public bool Login(Text.RealText.Login Log, Tempo Time)
        {
            try
            {
                String sql = "SELECT nome_user, senha_user, highscore_user, mortes_user, vitorias_user, tempo_user FROM tbUser WHERE nome_user = @name AND senha_user = @pass";
                MySqlCommand cmd = new MySqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@name", Log.username);
                cmd.Parameters.AddWithValue("@pass", Log.pass);
                con.Open();
                MySqlDataReader dr;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    if (dr["nome_user"].ToString() == Log.username && dr["senha_user"].ToString() == Log.pass)
                    {
                        Time.nome = Log.username;
                        Time.mortes = Convert.ToInt32(dr["mortes_user"]);
                        Time.highscore = Convert.ToInt32(dr["highscore_user"]);
                        Time.wins = Convert.ToInt32(dr["vitorias_user"]);
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw ex;
                Time.nome = "guest";
                Time.mortes = 0;
                Time.highscore = 0;
                Time.wins = 0;
            }
            finally
            {
                con.Close();
            }
            Time.nome = "guest";
            Time.mortes = 0;
            Time.highscore = 0;
            Time.wins = 0;
            return false;
        }

        public void Register(Text.RealText.Login Reg)
        {
            try
            {
                String sql = "insert into tbUser VALUES (@user, @pass, true, 0, 0, 0, 0);";
                MySqlCommand cmd = new MySqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@user", Reg.username);
                cmd.Parameters.AddWithValue("@pass", Reg.pass);
                con.Open();
                MySqlDataReader dr;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {

                }
            }
            catch(Exception ex)
            {
                //throw ex;
            }
            finally
            {
                con.Close();
            }
        }
    }
}
